package parent.cloud.order.util;

import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;

@Configuration  // 注册组件
public class ServiceInfoUtil implements ApplicationListener<WebServerInitializedEvent> {

    private static WebServerInitializedEvent event;

    /**
     * 声明event，用于获取运行服务器的本地端口号
     * @param event the event to respond to
     */
    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        ServiceInfoUtil.event = event;
    }

    public static int getPort() {
        int port = event.getSource().getPort();
        return port;
    }
}
